package cn.tedu.aiumall.passport.controller;

import cn.tedu.aiumall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.aiumall.passport.pojo.dto.AdminLoginInfoDTO;
import cn.tedu.aiumall.passport.pojo.dto.AdminUpdateDTO;
import cn.tedu.aiumall.passport.pojo.dto.AdminUpdatePasswordDTO;
import cn.tedu.aiumall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.aiumall.passport.security.LoginPrincipal;
import cn.tedu.aiumall.passport.sendSms.ISendMessageService;
import cn.tedu.aiumall.passport.service.IAdminService;
import cn.tedu.aiumall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admins")
@Api(tags = "01. 管理员管理模块")
public class AdminController {
    @Autowired
    IAdminService adminService;

    @Autowired
    ISendMessageService sendMessageService;

    @PostMapping("/sendMessage")
    @ApiOperation("发送验证码")
    @ApiOperationSupport(order = 80)
    public JsonResult<Void> sendMessage(AdminLoginInfoDTO adminLoginInfoDTO){
        log.debug("开始处理【发送手机验证码】的请求：参数：{}",adminLoginInfoDTO);
        String sendMessage=sendMessageService.sendMessage(adminLoginInfoDTO);
        log.debug("即将向客户端响应SendMessage数据：{}", sendMessage);
        return JsonResult.ok();
    }


    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 88)
    public JsonResult<String> login(AdminLoginInfoDTO adminLoginInfoDTO){
        log.debug("开始处理【登录认证】的请求，参数：{}", adminLoginInfoDTO);
        String jwt = adminService.login(adminLoginInfoDTO);
        log.debug("即将向客户端响应JWT数据：{}", jwt);
        return JsonResult.ok(jwt);
    }

    @PostMapping("/add-new")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperation("添加管理员")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO){
        log.info("开始处理【添加管理员】的请求,参数:{}",adminAddNewDTO);
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<List<AdminListItemVO>> list(
            @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理【查询管理员列表】的请求……");
        log.debug("从SecurityContext中获取到的当事人（当前JWT表示的用户）的id：{}", loginPrincipal.getId());
        log.debug("从SecurityContext中获取到的当事人（当前JWT表示的用户）的用户名：{}", loginPrincipal.getUsername());
        List<AdminListItemVO> list= adminService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation(value = "根据id删除管理员", notes = "请求头中必须携带名为Authorization的JWT，且对应的用户必须具有相关权限")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        log.info("开始根据id：{}处理删除管理员业务",id);
        adminService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/updatePassword")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperation("修改密码")
    @ApiOperationSupport(order = 400)
    public JsonResult<Void> updatePassword(AdminUpdatePasswordDTO adminUpdatePasswordDTO){
        log.info("adminUpdatePasswordDTO:{}",adminUpdatePasswordDTO);
        adminService.updatePassword(adminUpdatePasswordDTO);
        return JsonResult.ok();
    }
    @PostMapping("/updateInfo")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperation("修改管理员信息")
    @ApiOperationSupport(order = 410)
    public JsonResult<Void> updateInfo(Long id, AdminUpdateDTO adminUpdateDTO){
        log.info("开始根据id：{}处理修改管理员信息业务",id);
        log.info("adminUpdateDTO:{}",adminUpdateDTO);
        adminService.updateAdminInfo(id,adminUpdateDTO);
        return JsonResult.ok();
    }
    // http://localhost:9081/admins/5/enable
    @ApiOperation(value = "启用管理员账号", notes = "请求头中必须携带名为Authorization的JWT，且对应的用户必须具有相关权限")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用管理员账号】的请求：id={}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/5/disable
    @ApiOperation(value = "禁用管理员账号", notes = "请求头中必须携带名为Authorization的JWT，且对应的用户必须具有相关权限")
    @ApiOperationSupport(order = 510)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用管理员账号】的请求：id={}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }

}
